/**
 * 弹出层
 * 制作:  叶风
 * Qq:    355007778
 * Email: ye.fn@163.com 
 * @param {type} str
 * @param {type} nameTitle
 * @param {type} opt
 * @returns {box.Obj}
 */
function box(str, nameTitle, opt)
{	
	if (!opt)opt = {};
	var boxObj=$('<div>').appendTo('body').css('zIndex', 10000),
			Obj={finish: opt.finish || function(){} };
	
	
	// 遮挡层
	$('<div>').appendTo(boxObj)
		.dblclick(function(){Obj.close();})
		.fadeTo(0, 0.7)
		.css({position:"absolute",
				top:0,
				left:0,
				background:'#bbb',
				width:document.body.offsetWidth,
				height:Math.max($(document).height(), $(window).height())
		});	
	
	
	// 窗口
	var winObj = $('<div>').appendTo(boxObj)
					  .css({background :"#f3f3f3",
							border:"1px solid #269",
							position:"fixed",
							zIndex:9
							});
	
	// top
	var title=document.createElement("h5");
	$(title).appendTo(winObj)
			.css({padding: '2px 3px 5px',
				  margin:0,
				  background: '#369',
				  color: '#eee',
				  font:"12px Verdana, Geneva, Arial, Helvetica, sans-serif",
				  height: '13px'});
	
	if (nameTitle)
		title.innerHTML =  nameTitle;
	title.onmousedown = function(e){	
		if (!e) e=event;
		var x = e.screenX, y = e.screenY, l = parseInt(winObj[0].style.left), t = parseInt(winObj[0].style.top),d = $(document);
		d.bind('mousemove',mv).bind('mouseup', un);
		title.style.cursor = 'move';
		function mv(e){   
			window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();	
			winObj[0].style.left 		= e.screenX - x + l + 'px';
			winObj[0].style.top 		= e.screenY - y + t + 'px';
		}
		function un(){
			d.unbind('mousemove',mv).unbind('mouseup', un);
			title.style.cursor = '';
		}
	};
	
	// 按钮		
	var c = document.createElement("span");
	title.appendChild(c);
	c.innerHTML = language._('guan bi');
	$(c).css({color:'#cc0',
			  cursor:'pointer',
			  position:'absolute',
			  right:'3px'});
	Obj.close = c.onclick = function(t){
		function cc(){boxObj.remove();}
		
		if (typeof t=='number' && t>0)
			winObj.delay(t*1000).fadeTo(500, 0, cc);	
		else  
			winObj.animate({height:0,width:0}, 200, cc);
		return this;
	};
	  
	// content
	var txt = $('<div></div>').appendTo(winObj).append(str).css({
				overflow:'auto',
				minHeight: '18px',
				padding:'7px 10px 3px 10px',
				textAlign: 'center'});
	Obj.txt = txt;		

	
	// show
	var pl = $(window).width(),
		pt = $(window).height(),
		ow = 200;
	pl /= 2;
	pt /= 2;
	winObj.css({left:pl-100,top:pt-25});
	Obj.set = function(str, opt){ 
		if(!opt)opt = {};
		if (opt.align) txt.css('text-align',opt.align);

		txt.height('').html('').append(str);
		var w , h;
		if (opt.h){
			h = opt.h;
		}else{
			h =  Math.max(txt.height(), 20) + 30;
		}
		if (opt.w){
			w = opt.w;
		}else{
			winObj.width('');
			w =  Math.max(winObj.outerWidth(true)+20, 200);
			winObj.width(ow);
			ow = w;
		}

		var l = pl-w/2, t = pt-h/2;
		winObj.animate({left:l,top:t,width:w,height:h},200, function(){ txt.height(h-30); });
		return Obj;		
	};
	
	return Obj.set(str, opt);
}